﻿//
// XN4SL - Xna Framework for Silverlight.
//
using Xn4Sl.Framework.Graphics;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Media;

namespace Xn4Sl.Framework
{
    /// <summary>
    /// Handles the configuration and management of the graphics device.
    /// </summary>
    public class GraphicsDeviceManager : IDisposable, IGraphicsDeviceManager
    {
        /// <summary>
        ///  Content contains APIs that specifically relate to the Silverlight content area.
        /// </summary>
        private System.Windows.Interop.Content content;

        /// <summary>
        /// Specifies the default minimum back-buffer height.
        /// </summary>
        public static readonly int DefaultBackBufferHeight;

        /// <summary>
        /// Specifies the default minimum back-buffer width.
        /// </summary>
        public static readonly int DefaultBackBufferWidth;

        /// <summary>
        /// Creates a new GraphicsDeviceManager and registers it to handle the configuration
        /// and management of the graphics device for the specified Game.
        /// </summary>
        /// <param name="game">Game the GraphicsDeviceManager should be associated with.</param>
        public GraphicsDeviceManager(Game game)
        {
            this.graphicsDevice = game.GraphicsDevice;
            this.content = Application.Current.Host.Content;
            this.content.FullScreenChanged += new EventHandler(content_FullScreenChanged);
        }

        /// <summary>
        /// Occurs when the hosted Silverlight plug-in either enters or exits full-screen mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void content_FullScreenChanged(object sender, EventArgs e)
        {
            ScaleTransform sc = (ScaleTransform)this.graphicsDevice.UI.RenderTransform;

            if (this.content.IsFullScreen)
            {
                //sc.ScaleX = sc.ScaleY =
            }
            else
            {
                //sc.ScaleX = sc.ScaleY =
            }
        }

        /// <summary>
        /// Gets the Framework.Graphics.GraphicsDevice associated with the GraphicsDeviceManager.
        /// </summary>
        private GraphicsDevice graphicsDevice;
        public GraphicsDevice GraphicsDevice 
        { 
            get 
            {
                return this.graphicsDevice;
            } 
        }

        /// <summary>
        /// Gets or sets a value that indicates whether the device is in full-screen mode.
        /// </summary>
        public bool IsFullScreen 
        {
            get
            {
                return content.IsFullScreen;
            }
            set
            {
                content.IsFullScreen = value;
            }
        }

        /// <summary>
        /// Gets or sets the preferred back-buffer height.
        /// </summary>
        public int PreferredBackBufferHeight 
        {
            get
            {
                return this.graphicsDevice.Viewport.Height;
            }
            set
            {
                Viewport v = this.graphicsDevice.Viewport;
                v.Height = value;
                this.graphicsDevice.Viewport = v;
            }
        }

        /// <summary>
        /// Gets or sets the preferred back-buffer width.
        /// </summary>
        public int PreferredBackBufferWidth
        {
            get
            {
                return this.graphicsDevice.Viewport.Width;
            }
            set
            {
                Viewport v = this.graphicsDevice.Viewport;
                v.Width = value;
                this.graphicsDevice.Viewport = v;
            }
        }

        /// <summary>
        /// Applies any changes to device-related properties, changing the 
        /// graphics device as necessary.
        /// </summary>
        public void ApplyChanges()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Releases the unmanaged resources used by the GraphicsDeviceManager and optionally
        /// releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both automatic and manual resources; 
        /// false to release only manual resources.</param>
        protected virtual void Dispose(bool disposing) 
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Toggles between full screen and windowed mode.
        /// </summary>
        public void ToggleFullScreen()
        {
            throw new NotImplementedException();
        }

        #region IGraphicsDeviceManager Members

        public bool BeginDraw()
        {
            throw new NotImplementedException();
        }

        public void CreateDevice()
        {
            throw new NotImplementedException();
        }

        public void EndDraw()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
